﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EasyCore.UnifyResult.Results;

namespace EasyCore.DataValidation
{
    public class EasyValidationDefaultResultFactory:IEasyValidationResultFactory
    {
        public IActionResult CreateActionResult(ActionExecutingContext context, ValidationProblemDetails? validationProblemDetails)
        {
            Dictionary<string, object> errorData = new Dictionary<string, object>();
            if (validationProblemDetails != null)
            {
                foreach (var error in validationProblemDetails.Errors)
                {
                    errorData.Add(error.Key, error.Value[0]);
                }
            }
            var result = new EasyApiResult((int)EasyStatusCodes.ParametersError, "提交的数据不符合规则或存在数据校验失败！", errorData);
            return new JsonResult(result);
        }
    }
}
